<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.util.*" %>
<%@ page import="bean.Category" %>
<!DOCTYPE html>
<html>
<head>
    <title>新增分类</title>
    <style>
        .container {
            width: 600px;
            margin: 50px auto;
            padding: 20px;
            box-shadow: 0 0 10px rgba(0,0,0,0.1);
            border-radius: 5px;
        }
        
        .form-group {
            margin-bottom: 20px;
        }
        
        .form-group label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }
        
        .form-group input[type="text"],
        .form-group select,
        .form-group textarea {
            width: 100%;
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
        }
        
        .form-group textarea {
            height: 100px;
            resize: vertical;
        }
        
        .btn-group {
            margin-top: 20px;
            text-align: center;
        }
        
        .btn {
            padding: 10px 20px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            margin: 0 10px;
        }
        
        .btn-primary {
            background-color: #4CAF50;
            color: white;
        }
        
        .btn-secondary {
            background-color: #666;
            color: white;
        }
        
        .error-message {
            color: red;
            margin-bottom: 20px;
        }
    </style>
</head>
<body>
    <div class="container">
        <h2>新增分类</h2>
        
        <%-- 显示错误信息 --%>
        <% String error = (String)request.getAttribute("error");
           if(error != null && !error.isEmpty()) { %>
            <div class="error-message"><%= error %></div>
        <% } %>
        
        <form action="${pageContext.request.contextPath}/category" method="POST">
            <input type="hidden" name="action" value="addSubCategory">
            
            <div class="form-group">
                <label for="categoryId">所属大分类：</label>
                <select name="categoryId" id="categoryId" required>
                    <option value="">请选择大分类</option>
                    <% 
                        List<Category> categories = (List<Category>)request.getAttribute("categories");
                        Integer selectedCategoryId = (Integer)request.getAttribute("selectedCategoryId");
                        if(categories != null) {
                            for(Category category : categories) {
                    %>
                        <option value="<%= category.getId() %>" 
                            <%= (selectedCategoryId != null && selectedCategoryId.equals(category.getId())) ? "selected" : "" %>>
                            <%= category.getName() %>
                        </option>
                    <%
                            }
                        }
                    %>
                </select>
            </div>
            
            <div class="form-group">
                <label for="name">小分类名称：</label>
                <input type="text" id="name" name="name" required maxlength="50"
                    value="<%= request.getAttribute("name") != null ? request.getAttribute("name") : "" %>">
            </div>
            
            <div class="form-group">
                <label for="description">描述：</label>
                <textarea id="description" name="description" maxlength="200"><%=
                    request.getAttribute("description") != null ? request.getAttribute("description") : "" 
                %></textarea>
            </div>
            
            <div class="btn-group">
                <input type="submit" value="保存" class="btn btn-primary" name="submit">
                <button type="button" class="btn btn-secondary" onclick="window.location.href='category'">返回</button>
            </div>
        </form>
    </div>
</body>
</html> 